package uscdb.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import uscdb.dao.ProductDaoI;
import uscdb.model.Tproduct;
import uscdb.model.Tversion;
import uscdb.pageModel.DataGrid;
import uscdb.pageModel.Json;
import uscdb.pageModel.PageHelper;
import uscdb.pageModel.Product;
import uscdb.service.ProductServiceI;
import uscdb.util.StringUtil.StringUtil;

@Service
public class ProductServiceImpl implements ProductServiceI {

	@Autowired
	private ProductDaoI productDao;

	@Override
	public void add(Product product) {
		product.setId(UUID.randomUUID().toString());
		Tproduct tproduct = new Tproduct();
		BeanUtils.copyProperties(product, tproduct);
		productDao.save(tproduct);
	}

	@Override
	public void edit(Product product) {
		Tproduct tproduct = productDao.get(Tproduct.class, product.getId());
		if (null != tproduct) {
			BeanUtils.copyProperties(product, tproduct, new String[] { "id" });
		}
	}

	@Override
	public Json delete(String ids) {
		Json json = new Json();
		List<Tproduct> tproductList = new ArrayList<>();
		for (String id : ids.split(",")) {
			Tproduct tproduct = productDao.get(Tproduct.class, id);
			// 保存需要删除的 产品 集合
			tproductList.add(tproduct);
			Set<Tversion> tversionList = tproduct.getTversionSet();
			if (null != tversionList && tversionList.size() > 0) {
				json.setMsg("该产品[" + tproduct.getName() + "]下还有版本信息，不能被删除");
				return json;
			}
		}
		// 如果上面循环不返回，证明全部的产品下无版本信息，可以删除
		for (Tproduct tproduct : tproductList) {
			productDao.delete(tproduct);
		}
		json.setMsg("删除成功");
		json.setSuccess(true);
		return json;
	}

	@Override
	public DataGrid dataGrid(Product product, PageHelper ph) {
		DataGrid dg = new DataGrid();
		List<Product> productList = new ArrayList<>();
		List<Tproduct> tproductList = new ArrayList<>();

		Map<String, Object> params = new HashMap<>();
		String hql = "from Tproduct t ";
		tproductList = productDao.find(hql + orderHql(ph), params, ph.getPage(), ph.getRows());
		for (Tproduct tproduct : tproductList) {
			Product product2Page = new Product();
			BeanUtils.copyProperties(tproduct, product2Page);
			productList.add(product2Page);
		}
		dg.setTotal(productDao.count("select count(*) " + hql, params));
		dg.setRows(productList);
		return dg;

	}

	/**
	 * 排序函数
	 * 
	 * @param ph
	 *            分页对象
	 * @return
	 */
	private String orderHql(PageHelper ph) {
		String orderString = "";
		if (ph.getSort() != null && ph.getOrder() != null) {
			orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}

	@Override
	public List<Product> productCombobox(String q) {
		if (null == q || StringUtil.isEmpty(q)) {
			q = "";
		}
		Map<String, Object> params = new HashMap<>();
		params.put("name", "%%" + q.trim().toUpperCase() + "%%");
		String hql = "from Tproduct t where t.name like :name order by name";
		// 迟早是个bug
		List<Tproduct> tproductList = productDao.find(hql, params, 1, 20);
		List<Product> productList = new ArrayList<>();
		for (Tproduct tproduct : tproductList) {
			Product product = new Product();
			product.setName(tproduct.getName());
			product.setProductName(tproduct.getName());
			productList.add(product);
		}
		return productList;

	}

}
